/*
题目链接 : https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/description/?envType=daily-question&envId=2025-03-13
*/

//题解代码 : 
class Solution {
public:
    #define ll long long
    bool check(char c){
        return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';
    }
    long long countOfSubstrings(string word, int k) {
        int n = word.size();
        vector<int> amount(n,-1),less(n,-1);
        for(int i=0,j=0,cnt=0;j<n;++j){
            cnt += check(word[j])==false;
            while(cnt>=k){
                if(cnt==k && (check(word[i])==false)) break;
                cnt -= check(word[i++])==false;
            }
            if(cnt==k) amount[j] = i;
        }
        for(int i=0,j=0,cnt=0;j<n;++j){
            cnt += check(word[j])==false;
            while(cnt>k){
                cnt -= check(word[i++])==false;
            }
            if(cnt==k) less[j] = i;
        }

        ll ans = 0;
        vector<int> left(n,-1);
        int mp[26] = {0};
        for(int i=0,j=0,cnt=0;j<n;++j){
            if(check(word[j])){
                if(mp[word[j]-'a']++ == 0) ++cnt;
            }
            while(cnt==5){
                if(check(word[i])){
                    if(mp[word[i]-'a']==1) break;
                    mp[word[i]-'a']--;
                }
                ++i;
            }
            if(cnt==5) left[j] = i;
            if(left[j]>=0 && less[j]>=0 && left[j]>=less[j]){
                ans += min(left[j],amount[j]) - less[j] + 1;
            }
        }
        return ans;
    }
};
